package org.example.classtopia.Controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.example.classtopia.Common.Result.Result;
import org.example.classtopia.Model.vo.CollectionVO;
import org.example.classtopia.Model.vo.PageResultVO;
import org.example.classtopia.Service.PostCollectionsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@Tag(name = "收藏管理")
@RequestMapping("/collections")
public class PostCollectionsController {

    @Autowired
    private PostCollectionsService postCollectionsService;

    @Operation(summary = "收藏帖子")
    @PostMapping("/collect")
    public Result<?> CollectPost(@RequestParam Long postId, @RequestParam Long userId) {
        return postCollectionsService.CollectPost(postId, userId);
    }

    @Operation(summary = "取消收藏")
    @DeleteMapping("/uncollect")
    public Result<?> UnCollectPost(@RequestParam Long postId, @RequestParam Long userId) {
        return postCollectionsService.CancelCollectPost(postId, userId);
    }

    @Operation(summary = "按收藏时间降序获取收藏的帖子")
    @GetMapping("/get")
    public Result<PageResultVO<CollectionVO>> GetPostCollections(@RequestParam Long userId,
                                                                 @RequestParam(defaultValue = "1") int PageNum,
                                                                 @RequestParam(defaultValue = "10") int PageSize) {
        try{
            PageResultVO pageResultVO = postCollectionsService.FindAllPostByUserId(userId, PageNum, PageSize);
            return Result.success("查询成功！", pageResultVO);
        }
        catch (Exception e){
            return Result.error(500, e.getMessage());
        }
    }

    @Operation(summary = "测试是否收藏")
    @PostMapping("/iscollect")
    public Result<?> isCollectPost(@RequestParam Long postId, @RequestParam Long userId) {
        return postCollectionsService.isCollectPost(postId, userId);
    }
}
